﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace winproject.Models
{
    public class VirusDetails
    {
        public int nVirusId { get; set; }
        public string sVirusName { get; set; }
        public string sVirusSlug { get; set; }
        [AllowHtml]
        public string sVirusDescription { get; set; }
        public string sVirusImagePath { get; set; }
        public byte? nVirusStatus { get; set; }
    }
    public class VirusModel
    {
        #region Add New Virus Detail
        public ReturnResult AddNewVirus(VirusDetails newVirus,bool _isEditReq=false)
        {
            ReturnResult oResult = null;
            
            
            try
            {
                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                var oVirusupdate = oWinContext.VirusInfos.Where(c => c.VirusID == newVirus.nVirusId).FirstOrDefault();
                VirusInfo oVirus = new VirusInfo();
                if (oWinContext.DatabaseExists())
                {
                    oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.RecordNotInserted, msgJson = "OOps Something went wrong !!" };   
                    if (oVirusupdate != null)
                    {
                        //simply update virus info
                        oVirusupdate.VirusName = newVirus.sVirusName;
                        oVirusupdate.VirusSlug = newVirus.sVirusSlug;
                        oVirusupdate.VirusDescription = newVirus.sVirusDescription;
                        if (!string.IsNullOrEmpty(newVirus.sVirusImagePath) && Path.GetExtension(newVirus.sVirusImagePath).Length >= 3)
                        {
                            oVirusupdate.VirusImagePath = newVirus.sVirusImagePath;
                        }
                        if (!_isEditReq)
                            oVirusupdate.Status = 1;

                        oWinContext.SubmitChanges();
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = string.Format("Record {0} Successfully !!","Updated" ) };
                    }
                    else
                    {
                            
                        oVirus.VirusName = newVirus.sVirusName;
                        oVirus.VirusSlug = newVirus.sVirusSlug;
                        oVirus.VirusDescription = newVirus.sVirusDescription;
                        if (!string.IsNullOrEmpty(newVirus.sVirusImagePath) && Path.GetExtension(newVirus.sVirusImagePath).Length >= 3)
                        {
                            oVirus.VirusImagePath = newVirus.sVirusImagePath;
                        }
                        oVirus.Status = 1;
                        oWinContext.VirusInfos.InsertOnSubmit(oVirus);
                        oWinContext.SubmitChanges();
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = string.Format("Record {0} Successfully !!", "Added") };
                    }
                }
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
#endregion

        #region Find Virus Details - Fiilters : VirusSlug, VirusID, or Get ALL virus details
        public ReturnResult getVirusDetails(out List<VirusDetails> oVDetailList,VirusDetails oSearchItem=null)
        {
            ReturnResult oResult = null;
            oVDetailList = null;
            try
            {
                
                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                if (oWinContext.DatabaseExists())
                {
                    oVDetailList = new List<VirusDetails>();
                    if (oSearchItem != null && (!string.IsNullOrEmpty(oSearchItem.sVirusName) || !string.IsNullOrEmpty(oSearchItem.sVirusSlug)))
                    {
                        //search request
                        oVDetailList = oWinContext.VirusInfos.Where(c => c.VirusSlug == oSearchItem.sVirusSlug)
                                                                .Select(c => new VirusDetails() 
                                                                        {
                                                                            nVirusId= c.VirusID, 
                                                                            sVirusName= c.VirusName, 
                                                                            sVirusSlug= c.VirusSlug, 
                                                                            sVirusImagePath= c.VirusImagePath, 
                                                                            sVirusDescription= c.VirusDescription, 
                                                                            nVirusStatus= c.Status 
                                                                        }).ToList<VirusDetails>();
                    }
                    else if(oSearchItem != null && (oSearchItem.nVirusId)>0){
                        //search request
                        oVDetailList = oWinContext.VirusInfos.Where(c => c.VirusID == oSearchItem.nVirusId)
                                                                .Select(c => new VirusDetails()
                                                                {
                                                                    nVirusId = c.VirusID,
                                                                    sVirusName = c.VirusName,
                                                                    sVirusSlug = c.VirusSlug,
                                                                    sVirusImagePath = c.VirusImagePath,
                                                                    sVirusDescription = c.VirusDescription,
                                                                    nVirusStatus = c.Status
                                                                }).ToList<VirusDetails>();
                    }
                    else
                    {
                        oVDetailList = oWinContext.VirusInfos.Select(c => new VirusDetails()
                                                                {
                                                                    nVirusId = c.VirusID,
                                                                    sVirusName = c.VirusName,
                                                                    sVirusSlug = c.VirusSlug,
                                                                    sVirusImagePath = c.VirusImagePath,
                                                                    sVirusDescription = c.VirusDescription,
                                                                    nVirusStatus = c.Status
                                                                }).ToList<VirusDetails>();
                    }
                    
                    //var lsCategories = qCatList.ToList<Category>();
                    string resultJson = JsonConvert.SerializeObject(oVDetailList);
                    if (oVDetailList != null && oVDetailList.Count>0)
                    {
                        if (!string.IsNullOrEmpty(resultJson))
                        {
                            oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = resultJson };
                        }
                    }
                    else
                    {
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.NoRecordsFound, msgJson = resultJson };
                    }
                }
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
        #endregion

        #region Delete Virus Detail
        public ReturnResult deleteVirus(VirusDetails newVirus)
        {
            ReturnResult oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.BadRequest, msgJson = "[]" };
            try
            {
                if (newVirus.nVirusId > 0)
                {
                    VirusInfo oVirus = new VirusInfo();
                    oVirus.VirusID = newVirus.nVirusId;


                    var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                    if (oWinContext.DatabaseExists())
                    {
                        VirusInfo oVirInfo = oWinContext.VirusInfos.Where(x => x.VirusID == oVirus.VirusID).FirstOrDefault();
                        if (oVirInfo.VirusID > 0)
                        {
                            //means record exist
                            oWinContext.VirusInfos.DeleteOnSubmit(oVirInfo);
                            oWinContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
                            if (oVirus!=null && oVirus.VirusID > 0)
                            {
                                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = "Record removed successfully" };
                            }
                            else
                            {
                                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.RecordNotDeleted, msgJson = "No record deleted" };
                            }
                        }
                        else
                        {
                            oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.NoRecordsFound, msgJson = "No record found" };
                        }
                    }
                }
            }
            catch (ChangeConflictException e)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", e.ToString()) };
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
        #endregion

    }
}